#include "register.h"
#include "clientdatabase.h"
#include "ui_register.h"
#include "logindialog.h"
#include <QString>
#include <QSqlDatabase>
#include <QMouseEvent>
#include <QJsonDocument>
#include <QJsonObject>

Register::Register(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Register)
{
    tcpSocket1=new QTcpSocket(this);
    tcpSocket1->connectToHost("localhost",8022);
    connect(tcpSocket1, &QTcpSocket::connected, this, &Register::onConnected);
    connect(tcpSocket1, &QTcpSocket::disconnected, this, &Register::onDisconnected);
    connect(tcpSocket1, &QTcpSocket::readyRead, this, &Register::onReadyRead);

    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);

    if(ui->rs_acc_value->text().isEmpty()){
        ui->rs_acc_value->setPlaceholderText(tr("请输入至少1位字符"));
    }
    else{
        ui->rs_acc_value->setPlaceholderText(QString::null);
    }
    if(ui->rs_paw_value->text().isEmpty()){
        ui->rs_paw_value->setPlaceholderText(tr("请输入至少1位字符"));
    }
    else{
        ui->rs_paw_value->setPlaceholderText(QString::null);
    }
    if(ui->rs_name_value->text().isEmpty()){
        ui->rs_name_value->setPlaceholderText(tr("请输入至少1位字符"));
    }
    else{
        ui->rs_name_value->setPlaceholderText(QString::null);
    }

    ui->rs_paw_value->setStyleSheet("background:transparent;border-width:0;border-top:1px  solid;border-bottom:1px  solid;border-left:1px  solid;border-right: 1px  solid;border-style:outset");
    ui->rs_acc_value->setStyleSheet("background:transparent;border-width:0;border-top:1px  solid;border-bottom:1px  solid;border-left:1px  solid;border-right: 1px  solid;border-style:outset");
    ui->rs_name_value->setStyleSheet("background:transparent;border-width:0;border-top:1px  solid;border-bottom:1px  solid;border-left:1px  solid;border-right: 1px  solid;border-style:outset");
    ui->acc_repeated->clear();

    connect(ui->rs_acc_value,SIGNAL(textChanged(QString)),ui->acc_repeated,SLOT(acc_changed()));
}

Register::~Register()
{
    delete ui;
    delete tcpSocket1;
    close();
}

void Register::on_regist_clicked()
{
    QString str1=ui->rs_acc_value->text();
    QString str2=ui->rs_paw_value->text();
    QString str3=ui->rs_name_value->text();
    if(str1.length()<1||str2.length()<1||str3.length()<1){
          ui->acc_repeated->setText(tr("账号格式不正确"));
          return;
    }

    QJsonObject dataObject;
    dataObject["sendtype"]="register";
    dataObject["selfaccount"]=str1;
    dataObject["selfpassword"]=str2;
    dataObject["selfname"]=str3;
    QJsonDocument dataDocument(dataObject);
    QByteArray jsonData=dataDocument.toJson();
    tcpSocket1->write(jsonData);
}

void Register::mousePressEvent(QMouseEvent *event)
{
    this->windowPos = this->pos();       // 获得部件当前位置
    this->mousePos = event->globalPos(); // 获得鼠标位置
    this->dPos = mousePos - windowPos;   // 移动后部件所在的位置
}

void Register::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos() - this->dPos);
}

void Register::acc_changed(){
    ui->acc_repeated->clear();
}

void Register::on_toolButton_clicked()
{
   tcpSocket1->abort();
    this->close();
}

void Register::on_toolButton_2_clicked()
{
     this->showMinimized();
}

void Register::onConnected(){
    qDebug()<<"register on connected()"<<endl;
}

void Register::onDisconnected(){
    qDebug()<<"register on Disconnected()"<<endl;
}

void Register::onReadyRead(){
    QByteArray jsonData=tcpSocket1->readAll();
    QJsonDocument dataDocument=QJsonDocument::fromJson(jsonData);
    if(!dataDocument.isNull()&&dataDocument.isObject()){
        QJsonObject dataObject=dataDocument.object();
        if(dataObject["returntype"].toString()=="register"){
            if(dataObject["isRegister"].toBool()){
                ui->acc_repeated->setText(tr("注册成功"));
                clientDataBase::getInstance()->create_new_table(ui->rs_acc_value->text());
                this->close();
            }
            else{
                ui->acc_repeated->setText(tr("账号已被注册，请更改"));
            }
        }
    }
}
